package inventoryServe.controller;


import inventoryServe.po.Inventory;
import inventoryServe.po.Role;
import inventoryServe.po.Store;
import inventoryServe.po.Supplier;
import inventoryServe.service.ProductService;
import inventoryServe.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import inventoryServe.utils.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-22
 */
@Controller
public class StoreController {
    @Autowired
    private StoreService storeService;

    // 获取仓库列表数据
    @GetMapping("/stores")
    @ResponseBody
    public Object allStore(HttpServletRequest req) {
        String name = req.getParameter("name").trim();
        String address = req.getParameter("address").trim();

        Store store = new Store() ;
        if (name != "") {
            store.setName(name);
        }
        if (address != "") {
            store.setAddress(address);
        }
        List<Store> res = this.storeService.allStore(store);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }

    // 根据 id 获取仓库信息
    @GetMapping("/stores/{id}")
    @ResponseBody
    public Object storeOfId(@PathVariable("id") Integer id) {

        Store res = storeService.storeOfId(id);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }


    // 删除仓库
    @DeleteMapping("stores/{id}")
    @ResponseBody
    public Object deleteStore(@PathVariable("id") Integer id) {

        System.out.println("获取到了id " + id);

        boolean res = storeService.deleteStore(id);

        if (res) {
            return ResUtils.Success(1,"删除成功");
        } else {
            return ResUtils.Fail(0,"删除失败");
        }
    }

    // 增加仓库
    @PostMapping("stores/")
    @ResponseBody
    public Object addSupplier(@RequestBody Store store) {

        System.out.println(store);

        Date now = new Date();
        store.setCreateTime(now);
        store.setUpdateTime(now);

        System.out.println(store);

        boolean res = storeService.addStore(store);

        if (res) {
            return ResUtils.Success(1,"添加成功",store);
        } else {
            return ResUtils.Fail(0,"添加失败");
        }
    }


    // 根据 id 修改仓库信息
    @PutMapping("/stores/{id}")
    @ResponseBody
    public Object editStore(@PathVariable("id") Integer id, @RequestBody Store store) {

        Date now = new Date();
        store.setUpdateTime(now);

        System.out.println(store);

        Boolean res = storeService.editStore(store);

        if (res) {
            return ResUtils.Success(1, "修改成功",store);
        } else {
            return ResUtils.Fail(0, "修改失败");
        }
    }




}

